//
//  RSWelcomeViewController.swift
//  RSWeibo
//
//  Created by Macx on 16/8/11.
//  Copyright © 2016年 Macx. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit


class RSWelcomeViewController: UIViewController {
    
 // 北京图片
    private lazy var backgroundImgView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    // 头像图片框
    private lazy var iconImgView: UIImageView = {
    
        var imgView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        // 修改成圆的(倒圆角)
        imgView.layer.cornerRadius = 50
        
        // 切掉多余的
        imgView.layer.masksToBounds = true
        
        // 如果用户有头像, 就从服务器下载用户的头像
        if let iconStr = RSUserAccoutViewModel.sharedUserAccountViewModel.userAccountModel?.avatar_large {
        
            imgView.sd_setImageWithURL(NSURL(string: iconStr))
        }
        
        
        return imgView
    
    }()
    
    // 信息栏
    private lazy var msgLab: UILabel = {
    
         var msgLab = UILabel()
        
        // 设置字体颜色和大小
        msgLab.textColor = UIColor.blackColor()
        msgLab.font = UIFont.systemFontOfSize(16)
        
        // 决定怎么显示欢迎词
        if let name = RSUserAccoutViewModel.sharedUserAccountViewModel.userAccountModel?.name {
            msgLab.text = "欢迎回来, \(name)"
        } else {
            msgLab.text = "欢迎回来"
        }
        
        
        return msgLab
        
        
    }()
    
    
    override func loadView() {
        // 把视图 直接赋值
        view = backgroundImgView
        
        setupUI()
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

      
        // 开始动画
        startAnimation()
        
        
    }
    
    // 设置界面, 设置约束
    func setupUI(){
    
        view.addSubview(iconImgView)
        view.addSubview(msgLab)
        
        // 设置约束
        iconImgView.snp_makeConstraints { (make) in
            make.width.equalTo(100)
            make.height.equalTo(100)
            make.centerX.equalTo(view)
            make.centerY.equalTo(view.snp_top).offset(300)
        }
        
        msgLab.snp_makeConstraints { (make) in
            make.center.equalTo(view.snp_center)
        }
        
        
    }
    
    
    // 设置动画
    private func startAnimation(){
    
        // 更新约束
        msgLab.alpha = 0
        iconImgView.snp_updateConstraints { (make) in
            make.centerY.equalTo(view.snp_top).offset(100)
        }
        

        UIView.animateWithDuration(3, delay: 1, usingSpringWithDamping: 0.9, initialSpringVelocity: 1, options: [], animations: {
            
            self.view.layoutIfNeeded()
            self.msgLab.alpha = 1
            
            }) { (_) in
                
                
                // 动画结束的时候 通知切换根视图控制器
                NSNotificationCenter.defaultCenter().postNotificationName(changeRootViewController, object: self)
        }
        
    }
    
    
    deinit{
    
//        print("欢迎控制器没了")
    }

    
}




















